package com.zjhn.ds.config.mybatis;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.zjhn.ds.common.AuthenticationUser;
import com.zjhn.ds.common.Constant;
import com.zjhn.ds.utils.CommonUtil;
import com.zjhn.ds.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @BelongsProject: assetManage
 * @BelongsPackage: com.zjhn.admin.config
 * @Author: sxt
 * @CreateTime: 2023-05-27  14:41
 * @Description:
 * @Version: 1.0
 */
@Slf4j
@Configuration
@EnableTransactionManagement //开启事务管理
public class MyBatisPlusConfig {

    @Resource
    private UserUtil userUtil;

    /**
     * 注册插件
     * 如果用了分页插件注意先 add TenantLineInnerInterceptor 再 add PaginationInnerInterceptor
     *
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {

        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 多租户插件
        interceptor.addInnerInterceptor(
                new TenantLineInnerInterceptor(new TenantLineHandler() {
                    @Override
                    public Expression getTenantId() {
                        // 设置当前租户ID，实际情况你可以从cookie、或者缓存中拿都行
                        return new StringValue(userUtil.getUserInfo().getAbpTenantId());
                    }

                    @Override
                    public String getTenantIdColumn() {
                        // 对应数据库租户ID的列名
                        return "TenantId";
                    }

                    // 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件
                    @Override
                    public boolean ignoreTable(String tableName) {
                        // false 表示需要拼接多租户
                        List<String> list = Arrays.asList(Constant.TABLE_ARR);
                        if (list.contains(tableName)) {
                            if (Objects.nonNull(MybatisTenantContext.get())) {
//                                log.info("是否做租户隔离:{}",!MybatisTenantContext.get());
                                return MybatisTenantContext.get();
                            }
                            //默认租户隔离
                            return false;
                        }
                        // 未配置租户的表不需要做租户隔离
                        return true;
                    }
                })
        );
        // 分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.SQL_SERVER));
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

}
